package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/admin/category")
@Slf4j
@Api(tags = "分类管理相关接口")
public class CategoryController {
    @Autowired
    CategoryService categoryService;
    @ApiOperation("分类分页查询")
    @PostMapping("/page")
    public Result<PageResult> pageQuery(@RequestBody CategoryPageQueryDTO categoryPageQueryDTO){
        log.info(categoryPageQueryDTO.toString());
        PageResult pageResult = categoryService.pageQuery(categoryPageQueryDTO);
        return Result.success(pageResult);
    }
    @ApiOperation("新增分类管理")
    @PostMapping("/add")
    public Result add(@RequestBody CategoryDTO categoryDTO){
        categoryService.add(categoryDTO);
        return Result.success();
    }
    @ApiOperation("更新菜品分类")
    @PostMapping("/update")
    public Result update(@RequestBody CategoryDTO categoryDTO){
        categoryService.update(categoryDTO);
        return Result.success();
    }
    @ApiOperation("删除菜品分类")
    @GetMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        // 根据id查询
        Category category = categoryService.findOne(id);
        if(category == null){
            return Result.error("不存在id为"+id + "的菜品");
        }
        log.info("删除id->{}",id);
        // 删除id
        categoryService.delete(id);
        return Result.success();
    }
}
